import json

def get_stored_username():
    '''如果存储了用户名，就获取他。'''
    filename = 'username.json'
    try:
        with open(filename) as f:
            username = json.load(f)
    except FileNotFoundError:
        return None
    else:
        return username

def get_new_username():
    '''提示用户输入用户名'''
    username = input("What's your name?")
    filename = 'username.json'
    with open(filename,'w') as f:
        json.dump(username,f)
    return username

def greet_user():
    '''问候用户，并且指出他的名字'''
    username = get_stored_username()
    if username:
        if 'y'==input(f"Arr you {username}?y/n:"):
            print(f"Welcome back,{username}!")
        else:
            username = get_new_username()
            print(f"We'll remember you when you come back,{username}!")
    else:
        username = get_new_username()
        print(f"We'll remember you when you come back,{username}!")

greet_user()
